
#include <iostream>
#include <iomanip>
#include <random>
#include <cmath>

using namespace std;

// 定义随机数生成器
random_device dev;
mt19937 rng(dev());

// 生成指定范围内的随机小数
double generateRandomDouble(double from, double to, int precision) {
    uniform_real_distribution<double> dist(from, to);
    double randNum = dist(rng);
    return round(randNum * pow(10, precision)) / pow(10, precision);
}

// 生成随机数数组
void generateRandNumbers(double arr[], int n, double from, double to, int precision) {
    for (int i = 0; i < n; i++) {
        arr[i] = generateRandomDouble(from, to, precision);
    }
}

// 统计均值
double calculateMean(double arr[], int n) {
    double sum = 0;
    for (int i = 0; i < n; i++) {
        sum += arr[i];
    }
    return sum / n;
}

// 统计方差
double calculateVariance(double arr[], int n, double mean) {
    double sum = 0;
    for (int i = 0; i < n; i++) {
        sum += pow(arr[i] - mean, 2);
    }
    return sum / n;
}

int main() {
    double a, b; // 数值区间
    int precision; // 数值精度位数
    int n; // 随机数个数

    cout << "-----------随机数生成器---------" << endl;
    cout << "* 请输入数值区间a和b: ";
    cin >> a >> b;
    cout << "* 请输入数值精度位数: ";
    cin >> precision;
    cout << "* 请输入随机数的数目: ";
    cin >> n;
    cout << "-------------------------------" << endl;

    // 生成随机数数组
    double randNums[n];
    generateRandNumbers(randNums, n, a, b, precision);

    // 输出随机数
    cout << "随机数如下(每行输出5个数值):" << endl;
    for (int i = 0; i < n; i++) {
        cout << fixed << setprecision(precision) << randNums[i] << " ";
        if ((i + 1) % 5 == 0) {
            cout << endl;
        }
    }
    cout << endl;

    // 统计均值和方差
    double mean = calculateMean(randNums, n);
    double variance = calculateVariance(randNums, n, mean);

    // 输出结果
    cout << "均值: " << fixed << setprecision(precision) << mean << endl;
    cout << "方差: " << fixed << setprecision(precision) << variance << endl;

    // 基于均值和方差结果分析随机数分布特点
	cout<<"随机数分配比较随机，没看出啥规律"<<'\n';
   
    return 0;
}